package edu.cmu.lti.sepia.data.xml;

import java.io.IOException;
import java.io.InputStream;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

public class Deserializer {

  private IXmlHandler handler;
  
  public Deserializer(IXmlHandler handler) {
    this.handler = handler;
  }
  
  public IXmlSerializable deserialize(InputStream inputStream) throws Exception {
    IXmlSerializable result = null;
    try {
      XMLReader xmlReader = XMLReaderFactory.createXMLReader();
      xmlReader.setContentHandler(handler);
      xmlReader.parse(new InputSource(inputStream));
      result = handler.getParsed();
    } finally {
      try {
        inputStream.close();
      } catch (IOException e) {
      }
    }
    return result;
  }

}
